package com.jxau.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxau.controller.utils.Result;
import com.jxau.domain.User;
import com.jxau.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/MoviesWebsite/user")
public class UserController {
    @Resource
    private UserService userService;

    //获取所有用户信息
    @GetMapping
    public Result getAll(){
        return new Result(true, userService.list());
    }

    //添加用户
    @PostMapping()
    public Result save(@RequestBody User user){
        return new Result(userService.save(user));
    }

    //修改用户
    @PutMapping()
    public Result modify(@RequestBody User user){
        return new Result(userService.updateById(user));
    }

    //按用户id删除
    @DeleteMapping("{userid}")
    public Result delete(@PathVariable Integer userid){
        return new Result(userService.removeById(userid));
    }

    //按用户id查询
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        if(user != null){
            return new Result(true, user);
        }else{
            return new Result(false, null);
        }
    }

    //分页查询用户
    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, User user){
        IPage<User> page = userService.getPage(currentPage, pageSize, user);
        if(currentPage > page.getPages()){
            page = userService.getPage((int)page.getPages(), pageSize, user);
        }
        return new Result(true, page);
    }

}
